#include <stdio.h>
#include <string.h>
#include <time.h>
#include "sqlite3.h"
#define DBNAME "test.db"
int main()
{
	int i = 0;
	int rc = 0;
    char szSQL[1024];
	char *zErrMsg;
    sqlite3 *pdb;
	// 打开数据库
    rc = sqlite3_open(DBNAME, &pdb);
    if (rc != SQLITE_OK)
    {
        sqlite3_close(pdb);
        printf("无法打开数据库%s\n", DBNAME);
    }
	//	建表	
	rc = sqlite3_exec(pdb,"create table user(id int,name Text, birthday Text)",0,0,&zErrMsg); 
    if (rc != SQLITE_OK)
    {
        sqlite3_close(pdb);
        printf("无法创建表%s\n", DBNAME);
    }
#if 1

	time_t t1, t2; 
    time(&t1);
	// 开启事务
	rc = sqlite3_exec(pdb, "BEGIN TRANSACTION;", NULL, NULL, NULL);
    if( rc != SQLITE_OK )
    {
		printf("BEGIN TRANSACTION: %s\n", sqlite3_errmsg(pdb));
		sqlite3_close(pdb);
		return -1;
    }
	// 使用拼接字符串的方式插入100万条数据
	for (i = 0; i < 1000000; i++)
	{
		sprintf(szSQL, "insert into user(id, name, birthday) values (%d, '%s%d', '%s')", i + 1, "zhangshan", i + 1, "2011-09-01 23:59:59");
		rc = sqlite3_exec(pdb, szSQL, NULL, NULL, NULL);
		if (rc != SQLITE_OK)
		{
			printf("%s\n", sqlite3_errmsg(pdb));
			sqlite3_close(pdb);
			return -1;
		}

	}

	// 提交事务
	rc = sqlite3_exec(pdb, "COMMIT TRANSACTION;", NULL, NULL, NULL);

    if( rc != SQLITE_OK )
    {
		printf("COMMIT TRANSACTION: %s\n", sqlite3_errmsg(pdb));
		sqlite3_close(pdb);
		return -1;
    }
	time(&t2);
	printf("耗时： %ld 秒\n", t2 - t1);
    // 关闭数据库
#endif
    sqlite3_close(pdb);
    return 0;
}
